import json
import pytest
from fixture.fixture import *


def read_login(file):
    with open(file, 'r', encoding='utf-8') as f:
        data = json.load(f)
    return [(i['username'], i['password'], i['status']) for i in data]
def read_search(file):
    with open(file, 'r', encoding='utf-8') as f:
        data = json.load(f)
    return [(i['name'], i['status']) for i in data]
class Test_a3mall():
    token = None
    order_id = None
    @pytest.mark.parametrize('username,password,status',read_login(r'C:\Users\34098\PycharmProjects\zlyue1\data\login.json'))
    def test_login(self,username,password,status,api):
        data = {"username": username,"password": password}
        req = api.post(url='/api/public/login',status='json',data=data)
        print(req.json())
        if status == '正':
            token = req.json()['data']['token']
            Test_a3mall.token = {'auth-token':f"Bearer {token}"}

        else:
            print(status)
    @pytest.mark.parametrize('name,status',read_search(r'C:\Users\34098\PycharmProjects\zlyue1\data\search.json'))
    def test_srarch(self,name,status,api):
        req = api.get(f'/api/search/list?page=1&keywords={name}&type=0&sort=1')

        if status =='正':
            print(req.json())

        else:
            print(status)

    def test_cat(self,api):
        req = api.get(url = '/api/cart?page=1',headers=Test_a3mall.token)
        print(req.json())
        Test_a3mall.order_id = req.json()["data"]["list"][0]["id"]

    def test_buy(self,api):
        req = api.get(url=f'/api/order/confirm?id={Test_a3mall.order_id}&type=cart&shipping_type=1',headers=Test_a3mall.token)
        print(req.json())

    def test_zhif(self,api):
        data = {f"id":Test_a3mall.order_id,"type":"cart","address_id":21,"store_id":1,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":f"http://10.59.9.12:7001/wap/pages/cart/confirm?id={Test_a3mall.order_id}&type=cart"}
        req = api.post('/api/order/create',status='json',data=data,headers=Test_a3mall.token)
        print(req.json())





